package com.koron.car.web.lend.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.car.web.lend.bean.query.CarLendQuery;
import com.koron.car.web.lend.bean.vo.CarLendVO;
import com.koron.car.web.lend.service.CarLendService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (WCmCarLend)表控制层
 *
 * @author twcao
 * @since 2022-03-10 16:27:54
 */
@Slf4j
@RestController
@RequestMapping("/car/lend")
@Api(tags = "车辆管理-车辆租借")
public class CarLendController {
    /**
     * 服务对象
     */
    @Resource
    private CarLendService carLendService;

    @GetMapping("/selectOne")
    @ApiOperation("根据租借id查询信息")
    @ApiImplicitParam(name = "lendId", value = "租借id", required = true)
    public Response<CarLendVO> selectOne(String lendId) {
        log.info("根据租借id查询信息， lendId = {}", lendId);
        CarLendVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), carLendService, "queryById", CarLendVO.class, lendId);
        return Response.ok(bean);
    }

    @GetMapping("/queryList")
    @ApiOperation("根据条件查询车辆租借列表")
    public Response<PageInfo<CarLendVO>> queryList(CarLendQuery query) {
        log.info("根据条件查询车辆租借列表， query = {}", JSON.toJSONString(query));
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<CarLendVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), carLendService, "queryList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("创建/修改车辆租借")
    public Response<CarLendVO> saveOrUpdate(@RequestBody CarLendVO lend) {
        log.info("创建/修改车辆租借， lend = {}", JSON.toJSONString(lend));
        CarLendVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), carLendService, StringUtils.isBlank(lend.getId()) ? "insert" : "update", CarLendVO.class, lend);
        return Response.ok(bean);
    }
}
